#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0, right = 0;
        int zero = 0, count = 0;
        while (right < nums.size())
        {
            if (nums[right] != 1)
                zero++;
            while (zero > k)
            {
                count = max(right - left, count);
                if (nums[left++] == 0)
                    zero--;
            }
            right++;
        }
        return max(count, right - left);
    }
};